<?php
namespace app\system\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Model;

class Base extends  Controller
{
    protected $adminRules;
     //自加载
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //判断后台是否登录
        if(!session('aid')) {
            $this->redirect('login/index');
        }
        //权限管理
        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());

        //当前操作权限ID
        if(session('aid') != 1){
            $href_id = model('system_menu')->where('href',$controller.'/'.$action)->value('id');
            //当前管理员权限
            $group_id = model('Admin')->where('id', session('aid'))->value('group_id');
            $rules = model('AuthGroup')->where('group_id', $group_id)->value('rules');
            $this->adminRules = explode(',',$rules);
            if($href_id){
                if(!in_array($href_id, $this->adminRules)){
                    $this->error('您无此操作权限','index');
                }
            }
        }
        //该角色下可用菜单显示
        $href = $controller.'/'.$action;
        $nav = [];
        while($rule = model('system_menu')->where('href',$href)->find()){
            $nav[] = $rule['title'];
            $href = model('system_menu')->where('id',$rule['pid'])->value('href');
        }
        krsort($nav);
        $this->assign('nav',$nav);
    }
    //空操作
    public function _empty(){
        return $this->fetch('index/undefined');
    }
}